<template lang="pug">
data-page(
  v-bind="pageParams"
)
  mt-table(
    v-bind="tableProps"
  )
  sampleTrajectory(v-model="trajectoryShow" :selectionRow="specimenRow")
  templatePreview(v-model="templateShow" :selectRows="selectRow") 
</template>

<script setup lang="ts">
import { type SpecimenQueryParams } from "@scmt/api-lis-specimen";
import { type QueryPagination, useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type TableProps,
  useNotify,
  type Action,
} from "@scmt/common-components";
import sampleTrajectory from "../components/sampleTrajectory.vue";
import {
  ESpecimenStatus,
  ESpeimenTableColumnKey,
  ExecutionStatus,
  PatientType,
  specimenColumns,
} from "@scmt/api-common";
import templatePreview from "../components/templatePreview.vue";
import { useRequest } from "@scmt/common-hooks";
import { useUser } from "@scmt/common-store";
//添加 Props属性
const $api = useApi();
const user = useUser();
const $notify = useNotify();

const trajectoryShow = ref(false);
const specimenRow = ref({});
const selectionRow = ref([]);
const selectRow = ref({});
const templateShow = ref(false);
const emit = defineEmits(["message-to-parent"]);
const queryPramas = reactive<SpecimenQueryParams>({
  state: ESpecimenStatus.Verification,
  pageNum: 1,
  pageSize: 20,
});

const { request, data, loadingMap } = useRequest({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.lis.specimen.getSpecimenPageList(params).then((res) => {
      emit("message-to-parent", res);
      return res;
    });
  },
  batchSpecimenExamine: (q: any) => {
    return $api.lis.specimen.batchSpecimenExamine(q.specimenList).then(() => {
      $notify.success("审核成功");
      request("tableData", {
        pageSize: 20,
      });
      const specimenIdData = selectionRow.value.filter(
        (item) => item.source == PatientType.INPATIENT
      );
      if (specimenIdData && specimenIdData.length > 0) {
        let specimenIdList = specimenIdData.map((item) => {
          return item.sourceId;
        });
        if (specimenIdList && specimenIdList.length > 0) {
          request("updateExecutionStatus", {
            executionNumberList: specimenIdList,
            executionId: user.userId,
            executionName: user.username,
            executionStatus: ExecutionStatus.COMPLETED,
          });
        }
      }
    });
  },
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  updateExecutionStatus: (params: any) => {
    return $api.medical.medicalOrderExecution.updateExecutionStatus(params);
  },
});

onMounted(() => {
  request("tableData");
  request("listAsOptions");
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "inspectionTime",
      label: "检验时间",
      as: "date",
    },
    {
      prop: "barcode",
      label: "标本条码",
    },
    {
      prop: "deviceId",
      label: "检验仪器",
      as: "select",
      options: {
        loadMethod: () => $api.lis.instrument.listAsOptions(),
      },
    },
  ];

  const headerActions: Action[] = [
    {
      label: "批量审核",
      event: "examine",
      type: "primary",
      disabled: selectionRow?.value?.length == 0,
    },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    onQuery: () => request("tableData"),
    onAct: onPageActions,
    actions: headerActions,
    columns: 4,
  };

  return dataPageProps;
});

const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    insertIndex: false,
    columns: [
      {
        type: "selection",
      },
      specimenColumns[ESpeimenTableColumnKey.Source],
      specimenColumns[ESpeimenTableColumnKey.OutpatientNumber],
      specimenColumns[ESpeimenTableColumnKey.PatientName],
      specimenColumns[ESpeimenTableColumnKey.Sex],
      specimenColumns[ESpeimenTableColumnKey.Age],
      specimenColumns[ESpeimenTableColumnKey.ApplicationProject],
      specimenColumns[ESpeimenTableColumnKey.SpecimenTypeId],
      specimenColumns[ESpeimenTableColumnKey.Barcode],
      specimenColumns[ESpeimenTableColumnKey.State],
      specimenColumns[ESpeimenTableColumnKey.BillingDepartment],
      specimenColumns[ESpeimenTableColumnKey.BillingDoctor],
      specimenColumns[ESpeimenTableColumnKey.BillingTime],
      {
        prop: "signingTime",
        label: "签收时间",
        format: "datetime",
        sortable: true,
      },
      {
        label: "操作",
        fixed: "right",
        actions: [
          {
            label: "报告查看",
            event: "print",
            type: "primary",
          },
          {
            label: "报告打印",
            event: "print",
            type: "primary",
          },
          {
            label: "标本轨迹",
            event: "check",
            type: "primary",
          },
        ],
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) =>
      request("tableData", pagination),
    onSelectionChange: (selection: any) => {
      selectionRow.value = selection;
    },
  };
  return tableProps;
});
const onPageActions = (action: IAction, model?: any) => {
  switch (action.event) {
    case "add":
      break;
    case "destroy":
      break;
    case "examine":
      request("batchSpecimenExamine", {
        specimenList: selectionRow.value,
      });
      break;
    case "check":
      specimenRow.value = {};
      specimenRow.value = model;
      trajectoryShow.value = true;
      break;
    case "print":
      selectRow.value = {};
      selectRow.value = model;
      templateShow.value = true;
      break;
  }
};
</script>
